import sys



sys.path.insert(0, '../../../')

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'focus_on_rent.settings.dev')

import django
django.setup()

from django.http import JsonResponse
from users.models import User
from focus_on_rent.settings import dev
from homes.models import House
def fck(user_id):



    try:
        user = User.objects.filter(id=user_id)
    except Exception as e:
        return JsonResponse({'code': 4001,
                             'errmsg': '用户信息错误'})

    for use in user:
        username_avatar = dev.QINIU_URL + str(use.avatar)

    try:
        houses = House.objects.filter(user_id = user_id)
    except Exception as e:
        return JsonResponse({'code': 4002,
                             'errmsg': '没有房屋数据'})
    print(user)
    print(houses)


    house_list = []
    for house in houses:
        house_list.append({

            'address': house.address,
            'area_name': house.area_id,
            'ctime': house.create_time,
            'house_id': house.id,
            'img_url': dev.QINIU_URL + house.index_image_url,
            'order_count': house.order_count,
            'price': house.price,
            'room_count': house.room_count,
            'title': house.title,
            'user_avatar': username_avatar

        })

    print(username_avatar)
    hah = dev.QINIU_URL + house.index_image_url
    print(hah)

    data_house = {}
    data_house['houses'] = house_list
    print(house_list)
    return JsonResponse({'data': data_house,
                         'errmsg': 'ok',
                         'errno': 0})

if __name__ == '__main__':
    fck(1)